package com.jie.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.reggie.commen.CustomException;
import com.jie.reggie.dto.SetmealDto;
import com.jie.reggie.entity.Dish;
import com.jie.reggie.entity.Employee;
import com.jie.reggie.entity.Setmeal;
import com.jie.reggie.entity.SetmealDish;
import com.jie.reggie.mapper.EmployeeMapper;
import com.jie.reggie.mapper.SetmealMapper;
import com.jie.reggie.service.EmployeeService;
import com.jie.reggie.service.SetmealDishService;
import com.jie.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/02/15:15
 * @Description:
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * @Description: 新增套餐，同时保存套盒和菜品的关联关系
     * @Param: [setmealDto]
     * @return: void
     * @Author: 曾豪杰
     * @Date: 2022/9/3
     */
    public void saveWithDish(SetmealDto setmealDto) {

        //保存基本信息
        this.save(setmealDto);
        //保存菜品关系信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * @Description: 删除2张彪
     * @Param: [ids]
     * @return: void
     * @Author: 曾豪杰
     * @Date: 2022/9/3
     */
    public void removeWithDish(List<Long> ids) {
        //如果在销售不能删除，否则抛出异常
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);

        if(count>0){
            throw new CustomException("正在销售中，不能删除");
        }
        //list删除
        this.removeByIds(ids);

        //删除另一种表
        LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        //构造器删除
        setmealDishService.remove(queryWrapper1);

    }

    @Override
    public void updateWithDishStatus(List<Long> ids,int status) {
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> list = this.list(queryWrapper);
        for (Setmeal dish : list) {
            dish.setStatus(status);
            this.updateById(dish);
        }
    }
}
